from collections import OrderedDict

def ordered_dict():
    d = OrderedDict()
    d['f'] = 1
    d['b'] = 2
    d['c'] = 3
    d['d'] = 4
    d['f'] = 9

    for key in d:
        print(key, d[key])

    return d


d = ordered_dict()

# 用orderdict生成json数据
import json
print(json.dumps(d))


prices = { 'ACME': 45.23,
'AAPL': 612.78, 'IBM': 205.55, 'HPQ': 37.20, 'FB': 10.75
}

print(min(prices))
print(max(prices))

# 获取value最小值对应的key
print(min(prices, key=lambda k: prices[k]))
print(max(prices, key=lambda k: prices[k]))

# 获取对应的value值
min_value = prices[min(prices, key=lambda k: prices[k])]

# 使用zip函数颠倒key和value实现以value值进行比较找到最小值
print(min(zip(prices.values(), prices.keys())))
print(max(zip(prices.values(), prices.keys())))

print(sorted(zip(prices.values(), prices.keys())))


a={
'x' : 1,
'y' : 2,
'z' : 3
}
b={
'w' : 10,
'x' : 11,
'y' : 2
}

#交集
print(a.keys() & b.keys())
#a中存在b中不存在
print(a.keys() - b.keys())
#键值对的交集
print(a.items() & b.items())

#采用列表生成式，生成一个新的不包含指定key，value的字典
c = {key:a[key] for key in a.keys() - {'z', 'w'}}
print(c)

# 并集
print(a.keys() | b.keys())
# 差集
print(a.keys() ^ b.keys())
